<?php
// PANDA SIDEBAR TABS v.1.1
// www.pandathemes.com
add_action('init', 'widget_tabs_multi_register');
function widget_tabs_multi_register() {
	
	$prefix = 'tabs-multi'; // $id prefix
	$name = __('Panda - Tabs v.1.1');
	$widget_ops = array('classname' => 'widget_tabs_multi', 'description' => __('Comments, Tags, Categories, Archives, Pages. Up to 5 Tabs.'));
	$control_ops = array('width' => 200, 'height' => 200, 'id_base' => $prefix);
	
	$options = get_option('widget_tabs_multi');
	if(isset($options[0])) unset($options[0]);
	
	if(!empty($options)){
		foreach(array_keys($options) as $widget_number){
			wp_register_sidebar_widget($prefix.'-'.$widget_number, $name, 'widget_tabs_multi', $widget_ops, array( 'number' => $widget_number ));
			wp_register_widget_control($prefix.'-'.$widget_number, $name, 'widget_tabs_multi_control', $control_ops, array( 'number' => $widget_number ));

		}
	} else{
		$options = array();
		$widget_number = 1;
		wp_register_sidebar_widget($prefix.'-'.$widget_number, $name, 'widget_tabs_multi', $widget_ops, array( 'number' => $widget_number ));
		wp_register_widget_control($prefix.'-'.$widget_number, $name, 'widget_tabs_multi_control', $control_ops, array( 'number' => $widget_number ));
	}
}

function widget_tabs_multi($args, $vars = array()) {
    extract($args);
    $widget_number = (int)str_replace('tabs-multi-', '', @$widget_id);
    $options = get_option('widget_tabs_multi');
    if(!empty($options[$widget_number])){
    	$vars = $options[$widget_number];
    }

	$categories_order = $vars['categories_order'];
	$pages_order = $vars['pages_order'];
	$tags_order = $vars['tags_order'];
	$archives_order = $vars['archives_order'];
	$comments_order = $vars['comments_order'];

// START ARRAY
$pandatabs = array(
	$categories_order => array(
		"title"		=> $vars['categories_title']
	),
	$pages_order => array(
		"title"		=> $vars['pages_title']
	),
	$tags_order => array(
		"title"		=> $vars['tags_title']
	),
	$archives_order => array(
		"title"		=> $vars['archives_title']
	),
	$comments_order => array(
		"title"		=> $vars['comments_title']
	)
);
// SORT ARRAY
ksort($pandatabs);

    // WIDGET OUTPUT
		// echo $before_widget;
		// TITLE
		// if(!empty($vars['title'])){
		// echo $before_title . $vars['title'] . $after_title; } 
		// CONTENT
		?>

<li id="sidebar_tabs">
	<ul class="ui-tabs-nav list-none">
		<?php // TABS
		foreach ($pandatabs as $key => $value) {
		    if ($key>0) { ?>
				<li><a href="#fragment-<?php echo $key ?>"><span><?php echo $pandatabs[$key]["title"] ?></span></a></li>
			<?php ;}
		}
		?>
	</ul>

	<div id="flowpanes7" style="height:0;">
		<div id="archive" class="items">
			<?php // SLIDES
			foreach ($pandatabs as $key => $value) {
			    if ($key>0) { ?>
				<div class="item">
					<div id="fragment-<?php echo $key ?>">

						<?php // CATEGORIES
						if ($pandatabs[$key]["title"]==$vars['categories_title']) { ?>
							<ul id="widget-list">
								<?php wp_list_categories("depth=3&title_li=&hide_empty=1&show_count=0&exclude=".$vars['categories_exclude']); ?>
							</ul>
						<?php } ?>

						<?php // PAGES
						if ($pandatabs[$key]["title"]==$vars['pages_title']) { ?>
							<ul id="widget-list" class="pgs">
								<?php wp_list_pages("depth=3&title_li=&exclude=".$vars['pages_exclude']); ?>
							</ul>
						<?php } ?>

						<?php // TAGS
						if ($pandatabs[$key]["title"]==$vars['tags_title']) { ?>
							<ul id="widget-list-tags">
								<li>
									<?php wp_tag_cloud(); ?> 
								</li>
							</ul>
						<?php } ?>

						<?php // ARCHIVES
						if ($pandatabs[$key]["title"]==$vars['archives_title']) { ?>
							<ul id="widget-list">
								<?php wp_get_archives('type=monthly'); ?>
							</ul>
						<?php } ?>

						<?php // COMMENTS
						if ($pandatabs[$key]["title"]==$vars['comments_title']) { ?>
							<?php
								global $wpdb, $comments, $comment;
								$number = (5);
								$gsize = (32);
								$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");

								echo '<ul id="comments-widget">';
								foreach ( $comments as $comment ) {
									$cpID = $comment->comment_post_ID;
									$cptitle = get_the_title( $cpID );
									$cplink  = get_comment_link( $comment->comment_ID );
									$cpcount = get_comments_number( $cpID );
									$comment_content = strip_tags($comment->comment_content);
									$comment_content = stripslashes($comment_content);
									$words=split(" ",$comment_content); 
									$comment_excerpt = join(" ",array_slice($words,0,15));
									if ( function_exists( 'get_avatar' ) ) {
										$author_gravatar = get_avatar( $comment->comment_author_email, $gsize ); }
									else {
										$author_gravatar = '<img src="' . plugins_url( 'author.png', __FILE__ ) . 'width="' . $gsize . '" alt="Author" />'; }
									echo "<li>{$author_gravatar} <strong>"; comment_author_link();
									echo "</strong>&nbsp;on <a href='{$cplink}'>{$cptitle}</a>&nbsp;<small>({$cpcount})</small>"; 
									echo "<p>".$comment_excerpt."</p></li>";
									}
								echo '</ul>';
							?>
						<?php } ?>

						<div class="clear"><!-- --></div>
					</div>
				</div>
				<?php ;}
			}
			?>
		</div>
	</div><!-- end flowpanes -->
	<div class="clear"><!-- --></div>
</li>
		<?php
		// echo $after_widget;
}

function widget_tabs_multi_control($args) {

	$prefix = 'tabs-multi'; // $id prefix
	
	$options = get_option('widget_tabs_multi');
	if(empty($options)) $options = array();
	if(isset($options[0])) unset($options[0]);
		
	// update options array
	if(!empty($_POST[$prefix]) && is_array($_POST)){
		foreach($_POST[$prefix] as $widget_number => $values){
			if(empty($values) && isset($options[$widget_number])) // user clicked cancel
				continue;
			
			if(!isset($options[$widget_number]) && $args['number'] == -1){
				$args['number'] = $widget_number;
				$options['last_number'] = $widget_number;
			}
			$options[$widget_number] = $values;
		}
		
		// update number
		if($args['number'] == -1 && !empty($options['last_number'])){
			$args['number'] = $options['last_number'];
		}

		// clear unused options and update options in DB. return actual options array
		$options = bf_smart_multiwidget_update($prefix, $options, $_POST[$prefix], $_POST['sidebar'], 'widget_tabs_multi');
	}
	
	// $number - is dynamic number for multi widget, gived by WP
	// by default $number = -1 (if no widgets activated). In this case we should use %i% for inputs
	//   to allow WP generate number automatically
	$number = ($args['number'] == -1)? '%i%' : $args['number'];

	// now we can output control
	$opts = @$options[$number];
	$title = @$opts['title'];
	// CATEGORIES
	$categories_order = @$opts['categories_order'];
	$categories_title = @$opts['categories_title'];
	$categories_exclude = @$opts['categories_exclude'];
	// PAGES
	$pages_order = @$opts['pages_order'];
	$pages_title = @$opts['pages_title'];
	$pages_exclude = @$opts['pages_exclude'];
	// TAGS
	$tags_order = @$opts['tags_order'];
	$tags_title = @$opts['tags_title'];
	// ARCHIVES
	$archives_order = @$opts['archives_order'];
	$archives_title = @$opts['archives_title'];
	// COMMENTS
	$comments_order = @$opts['comments_order'];
	$comments_title = @$opts['comments_title'];
	?>
	<input style="display:none;" type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][title]" value="<?php echo $title; ?>" />

	<table style="width:100%">
		<tr>
			<td><small>Order:</small></td>
			<td><small>Title:</small></td>
			<td><small>Exclude:</small></td>
		</tr>
		<tr>
			<td>
				<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][categories_order]">
					<option value="0" <?php if ($categories_order=='0'):?> selected <?php endif; ?>>hide</option>
					<option value="1" <?php if ($categories_order=='1'):?> selected <?php endif; ?>>1</option>
					<option value="2" <?php if ($categories_order=='2'):?> selected <?php endif; ?>>2</option>
					<option value="3" <?php if ($categories_order=='3'):?> selected <?php endif; ?>>3</option>
					<option value="4" <?php if ($categories_order=='4'):?> selected <?php endif; ?>>4</option>
					<option value="5" <?php if ($categories_order=='5'):?> selected <?php endif; ?>>5</option>
				</select>
			</td>
			<td>
				<input id="<?php echo $prefix; ?>[<?php echo $number; ?>][categories_title]" name="<?php echo $prefix; ?>[<?php echo $number; ?>][categories_title]" type="text" class="widefat" value="<?php if (!$categories_title) { echo "Categories" ;} else { echo $categories_title;} ?>" />
			</td>
			<td>
				<input id="<?php echo $prefix; ?>[<?php echo $number; ?>][categories_exclude]" name="<?php echo $prefix; ?>[<?php echo $number; ?>][categories_exclude]" type="text" class="widefat" value="<?php echo $categories_exclude; ?>" />
			</td>
		</tr>
		<tr>
			<td>
				<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][pages_order]">
					<option value="0" <?php if ($pages_order=='0'):?> selected <?php endif; ?>>hide</option>
					<option value="1" <?php if ($pages_order=='1'):?> selected <?php endif; ?>>1</option>
					<option value="2" <?php if ($pages_order=='2'):?> selected <?php endif; ?>>2</option>
					<option value="3" <?php if ($pages_order=='3'):?> selected <?php endif; ?>>3</option>
					<option value="4" <?php if ($pages_order=='4'):?> selected <?php endif; ?>>4</option>
					<option value="5" <?php if ($pages_order=='5'):?> selected <?php endif; ?>>5</option>
				</select>
			</td>
			<td>
				<input id="<?php echo $prefix; ?>[<?php echo $number; ?>][pages_title]" name="<?php echo $prefix; ?>[<?php echo $number; ?>][pages_title]" type="text" class="widefat" value="<?php if (!$pages_title) { echo "Pages" ;} else { echo $pages_title;} ?>" />
			</td>
			<td>
				<input id="<?php echo $prefix; ?>[<?php echo $number; ?>][pages_exclude]" name="<?php echo $prefix; ?>[<?php echo $number; ?>][pages_exclude]" type="text" class="widefat" value="<?php echo $pages_exclude; ?>" />
			</td>
		</tr>
		<tr>
			<td>
				<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][tags_order]">
					<option value="0" <?php if ($tags_order=='0'):?> selected <?php endif; ?>>hide</option>
					<option value="1" <?php if ($tags_order=='1'):?> selected <?php endif; ?>>1</option>
					<option value="2" <?php if ($tags_order=='2'):?> selected <?php endif; ?>>2</option>
					<option value="3" <?php if ($tags_order=='3'):?> selected <?php endif; ?>>3</option>
					<option value="4" <?php if ($tags_order=='4'):?> selected <?php endif; ?>>4</option>
					<option value="5" <?php if ($tags_order=='5'):?> selected <?php endif; ?>>5</option>
				</select>
			</td>
			<td>
				<input id="<?php echo $prefix; ?>[<?php echo $number; ?>][tags_title]" name="<?php echo $prefix; ?>[<?php echo $number; ?>][tags_title]" type="text" class="widefat" value="<?php if (!$tags_title) { echo "Tags" ;} else { echo $tags_title;} ?>" />
			</td>
			<td>&nbsp;
			</td>
		</tr>
		<tr>
			<td>
				<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][archives_order]">
					<option value="0" <?php if ($archives_order=='0'):?> selected <?php endif; ?>>hide</option>
					<option value="1" <?php if ($archives_order=='1'):?> selected <?php endif; ?>>1</option>
					<option value="2" <?php if ($archives_order=='2'):?> selected <?php endif; ?>>2</option>
					<option value="3" <?php if ($archives_order=='3'):?> selected <?php endif; ?>>3</option>
					<option value="4" <?php if ($archives_order=='4'):?> selected <?php endif; ?>>4</option>
					<option value="5" <?php if ($archives_order=='5'):?> selected <?php endif; ?>>5</option>				
				</select>
			</td>
			<td>
				<input id="<?php echo $prefix; ?>[<?php echo $number; ?>][archives_title]" name="<?php echo $prefix; ?>[<?php echo $number; ?>][archives_title]" type="text" class="widefat" value="<?php if (!$archives_title) { echo "Archives" ;} else { echo $archives_title;} ?>" />
			</td>
			<td>&nbsp;
			</td>
		</tr>
		<tr>
			<td>
				<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][comments_order]">
					<option value="0" <?php if ($comments_order=='0'):?> selected <?php endif; ?>>hide</option>
					<option value="1" <?php if ($comments_order=='1'):?> selected <?php endif; ?>>1</option>
					<option value="2" <?php if ($comments_order=='2'):?> selected <?php endif; ?>>2</option>
					<option value="3" <?php if ($comments_order=='3'):?> selected <?php endif; ?>>3</option>
					<option value="4" <?php if ($comments_order=='4'):?> selected <?php endif; ?>>4</option>
					<option value="5" <?php if ($comments_order=='5'):?> selected <?php endif; ?>>5</option>	
				</select>
			</td>
			<td>
				<input id="<?php echo $prefix; ?>[<?php echo $number; ?>][comments_title]" name="<?php echo $prefix; ?>[<?php echo $number; ?>][comments_title]" type="text" class="widefat" value="<?php if (!$comments_title) { echo "Comments" ;} else { echo $comments_title;} ?>" />
			</td>
			<td>&nbsp;
			</td>
		</tr>
	</table>
	
	<p>
		<br />
		<small>Exclude by ID e.g.&nbsp;1,2,3,4</small><br /><br />
		<small>The widget may be used unlimited times on the website but once on the one page.</small>
	</p>

	<?php
}

// helper function can be defined in another plugin
if(!function_exists('bf_smart_multiwidget_update')){
	function bf_smart_multiwidget_update($id_prefix, $options, $post, $sidebar, $option_name = ''){
		global $wp_registered_widgets;
		static $updated = false;

		// get active sidebar
		$sidebars_widgets = wp_get_sidebars_widgets();
		if ( isset($sidebars_widgets[$sidebar]) )
			$this_sidebar =& $sidebars_widgets[$sidebar];
		else
			$this_sidebar = array();
		
		// search unused options
		foreach ( $this_sidebar as $_widget_id ) {
			if(preg_match('/'.$id_prefix.'-([0-9]+)/i', $_widget_id, $match)){
				$widget_number = $match[1];
				
				// $_POST['widget-id'] contain current widgets set for current sidebar
				// $this_sidebar is not updated yet, so we can determine which was deleted
				if(!in_array($match[0], $_POST['widget-id'])){
					unset($options[$widget_number]);
				}
			}
		}
		
		// update database
		if(!empty($option_name)){
			update_option($option_name, $options);
			$updated = true;
		}
		
		// return updated array
		return $options;
	}
}
?>